//package com.laiketui.payment.controller;
//
//import com.google.common.collect.Maps;
//import com.laiketui.core.domain.Result;
//import com.laiketui.core.exception.LaiKeAPIException;
//import com.laiketui.domain.vo.MainVo;
//import com.laiketui.payment.api.WechatPayService;
//import com.laiketui.core.cache.RedisUtil;
//import com.laiketui.core.lktconst.GloabConst;
//import com.laiketui.root.annotation.HttpApiMethod;
//import io.swagger.annotations.ApiOperation;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletRequest;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.security.GeneralSecurityException;
//import java.text.ParseException;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.stream.Collectors;
//import java.util.stream.Stream;
//
///**
// * @author wangxian
// */
//@RestController
//public class WechatPayV3NotifyController {
//
//    private final Logger logger = LoggerFactory.getLogger(WechatPayV3NotifyController.class);
//
//    @Autowired
//    private WechatPayService wechatPayServiceV3DubboImpl;
//
//    @Autowired
//    RedisUtil redisUtil;
//
//    @RequestMapping("/v3/weChatNotify")
//    public Map  weChatNotifyV3(HttpServletRequest request){
//        Map  map = new HashMap<>(2);
//        InputStream inStream = null;
//        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
//        try {
//            /**微信返回的请求体*/
//            inStream = request.getInputStream();
//            byte[] buffer = new byte[1024];
//            int len = 0;
//            while ((len = inStream.read(buffer)) != -1) {
//                outSteam.write(buffer, 0, len);
//            }
//            /** 获取微信调用notify_url的返回XML信息 */
//            String notifyJson = new String(outSteam.toByteArray(), GloabConst.Chartset.UTF_8);
//
//            logger.info("微信v3回调返回结果");
//            StringBuffer url = request.getRequestURL();
//            String domain = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
//            //从redis里面获取这个密钥  TODO 缓存 商户二级域名和密钥对应
//            String wxPayV3Key = (String) redisUtil.get(domain);
//
//            /**如果验证签名序列号通过*/
//            Map params = Maps.newHashMap();
//            params.put("notifyJson",notifyJson);
//            params.put("wxPayV3Key",wxPayV3Key);
//            map = wechatPayServiceV3DubboImpl.payBack(params);
//        } catch (IOException e) {
//            e.printStackTrace();
//            e.printStackTrace();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            if (outSteam!=null){
//                try {
//                    outSteam.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//            if (inStream!=null){
//                try {
//                    inStream.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        return map;
//    }
//
//    /**
//     * 验证微信签名
//     * @param request
//     * @param notifyjson
//     * @param apiV3Key
//     * @return
//     * @throws GeneralSecurityException
//     * @throws IOException
//     * @throws InstantiationException
//     * @throws IllegalAccessException
//     * @throws ParseException
//     */
//    private boolean verifiedSign(HttpServletRequest request,String notifyjson,String apiV3Key) throws GeneralSecurityException, IOException, InstantiationException, IllegalAccessException, ParseException {
//        /**微信返回的证书序列号*/
//        String serialNo = request.getHeader("Wechatpay-Serial");
//        //微信返回的随机字符串
//        String nonceStr = request.getHeader("Wechatpay-Nonce");
//        //微信返回的时间戳
//        String timestamp = request.getHeader("Wechatpay-Timestamp");
//        //微信返回的签名
//        String wechatSign = request.getHeader("Wechatpay-Signature");
//        //组装签名字符串
//        String signStr = Stream.of(timestamp, nonceStr, notifyjson).collect(Collectors.joining("\n", "", "\n"));
//        try {
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//
//    @ApiOperation("商家转账到零钱")
//    @RequestMapping("/v3/MerchantTransfersToChange")
//    @HttpApiMethod(urlMapping = {"app.v3.MerchantTransfersToChange"})
//    public Result MerchantTransfersToChange(String paramJson) {
//        try {
//            return Result.success(wechatPayServiceV3DubboImpl.MerchantTransfersToChange(paramJson));
//        }catch (LaiKeAPIException e){
//            return Result.error(e.getCode(), e.getMessage(), e.getMethod());
//        }
//    }
//
//    @ApiOperation("商家转账到零钱查询批次转账单结果")
//    @RequestMapping("/v3/queryBatchTransferOrder")
//    @HttpApiMethod(urlMapping = {"app.v3.QueryBatchTransferOrder"})
//    public Result QueryBatchTransferOrder(MainVo vo) {
//        try {
//            wechatPayServiceV3DubboImpl.QueryBatchTransferOrder(vo);
//            return Result.success(GloabConst.ManaValue.MANA_VALUE_SUCCESS);
//        }catch (LaiKeAPIException e){
//            return Result.error(e.getCode(), e.getMessage(), e.getMethod());
//        }
//    }
//
//
//}
